package org.odk.clinic.adroid.service;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.odk.clinic.android.openmrs.Constants;

public class Demographic {
	
	public ArrayList<String> GetAllDemographic(int idpatient){
		ArrayList<String> demographic = new ArrayList<String>();
		int NumCount;
		String Valueresult = null;
		String address1, address2, city_village, state_province, country, postal_code, latitude, longitude = null;
		//Dictionary<String, String> obser = null;
		String ServicePage = "Demographic.asmx";
		String METHOD_NAME = "GetAllDemographics";
		SoapObject responseBody = null; //Content file XML
		SoapObject table = null; // Table hold data get back form service
		SoapObject tableRow = null; // Row of table
		
		SoapObject Request = new SoapObject(Constants.NAMESPACE, METHOD_NAME);
		Request.addProperty("idpatient", idpatient);
		SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelop.dotNet = true;
		envelop.encodingStyle = SoapSerializationEnvelope.ENC;
		envelop.setOutputSoapObject(Request);
		AndroidHttpTransport transport = new AndroidHttpTransport(Constants.URL + ServicePage);
		try{
			transport.call(Constants.SOAP_ACTION + METHOD_NAME, envelop);
			SoapObject result = (SoapObject)envelop.bodyIn;
			try{
				if(result.getPropertyCount() > 0 )
				{
					//Get all file XML
					responseBody = (SoapObject)envelop.getResponse();
					//Reject information XML and get back result
					responseBody = (SoapObject)responseBody.getProperty(0);
					//Get information of back table
					//table = (SoapObject)responseBody.getProperty(0);
					//Count records in file XML
					//NumCount = table.getPropertyCount();
					//for(int i = 0; i < NumCount; i++)
					//{
						//tableRow = (SoapObject)table.getProperty(i);
						//int hehe = tableRow.getProperty("address1").toString().length();
						//if(tableRow.getProperty("address1").toString().length() == 9)
							//address1 = "Not Info";
						//else
							address1 = responseBody.getProperty("Address1").toString();
						
						//if(tableRow.getProperty("address2").toString().length() == 9)
							//address2 = "Not Info";
						//else
							address2 = responseBody.getProperty("Address2").toString();
						
						//if(tableRow.getProperty("city_village").toString().length() == 9)
							//city_village = "Not Info";
						//else
							city_village = responseBody.getProperty("CityVillage").toString();
						
						//if(tableRow.getProperty("state_province").toString().length() == 9)
							//state_province = "Not Info";
						//else
							state_province = responseBody.getProperty("StateProvince").toString();
						
						//if(tableRow.getProperty("country").toString().length() == 9)
							//country = "Not Info";
						//else
							country = responseBody.getProperty("Country").toString();
						
						//if(tableRow.getProperty("postal_code").toString().length() == 9)
							//postal_code = "Not Info";
						//else
							postal_code = responseBody.getProperty("PostalCode").toString();
						
						
						//if(tableRow.getProperty("latitude").toString().length() == 9)
							//latitude = "Not Info";
						//else
							latitude = responseBody.getProperty("Latitude").toString();
						
						//if(tableRow.getProperty("longitude").toString().length() == 9)
							//longitude = "Not Info";
						//else
							longitude = responseBody.getProperty("Longitude").toString();
						
						Valueresult = address1 + "@" + address2 + "@" + city_village + "@" + state_province + "@" + country +
								"@" + postal_code + "@" + latitude + "@" + longitude;
									
						demographic.add(Valueresult);
						
					}
				//}
				return demographic;
			}catch(Exception ex){
				ex.printStackTrace();
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}
